package shop.up.item.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import shop.up.item.service.ItemService;
import shop.up.list.client.ListFeignClient;
import shop.up.model.product.BaseCategoryView;
import shop.up.model.product.SkuInfo;
import shop.up.model.product.SpuSaleAttr;
import shop.up.product.client.ProductFeignClient;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author zzc
 * @date 2023/4/2 15:00
 */
@Service
@Slf4j
public class ItemServiceImpl implements ItemService {

    @Resource
    private ProductFeignClient productFeignClient; // 远程调用

    @Resource
    private ListFeignClient listFeignClient;

    @Resource
    private ThreadPoolExecutor threadPoolExecutor; // 注入线程池

    /**
     * 获取 sku 详情信息
     *
     * @param skuId
     * @return
     */
    @Override
    public Map<String, Object> getBySkuId(Long skuId) {
        HashMap<String, Object> result = new HashMap<>();

        // 通过skuId 查询skuInfo
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            // 保存skuInfo
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        // 销售属性-销售属性值回显并锁定
        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            // 保存 spuSaleAttrList
            result.put("spuSaleAttrList", spuSaleAttrList);
        }, threadPoolExecutor);

        // 根据spuId 查询 map 集合属性
        CompletableFuture<Void> skuValueIdsMapCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            // 保存json 字符串
            String valuesSkuJson = JSON.toJSONString(skuValueIdsMap);
            result.put("valuesSkuJson", valuesSkuJson);
        }, threadPoolExecutor);

        // 获取商品最新价格
        CompletableFuture<Void> skuPriceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            // 保存价格
            result.put("price", skuPrice);
        });

        // 获取商品分类
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            // 保存商品分类数据
            result.put("categoryView", categoryView);
        });

        //更新商品incrHotScore
        CompletableFuture<Void> incrHotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);

        // 等待所有线程执行完，返回结果
        CompletableFuture.allOf(skuInfoCompletableFuture,
                        spuSaleAttrListCompletableFuture, skuPriceCompletableFuture,
                        skuValueIdsMapCompletableFuture, categoryViewCompletableFuture,
                        incrHotScoreCompletableFuture)
                .join();
        return result;
    }
}
